<!-- 三级分类全局组件 -->
<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c1Id"
          @change="handlerChange"
        >
          <el-option
            v-for="c1 in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c2Id"
          @change="handlerChange2"
        >
          <el-option
            v-for="c2 in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene == 0 ? false : true"
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="c3 in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
//引入组件挂载完毕方法
import { onMounted } from 'vue';
//引入分类相关的仓库
import useCategoryStore from '@/store/modules/category';
let categoryStore = useCategoryStore();

//加载时清空一下仓库
categoryStore.$reset();

//接受场景值
defineProps(['scene']);

//分类全局组件挂载完毕,通知仓库发请求获取一级分类的数据
onMounted(() => {
  getC1();
});
//通知仓库获取一级分类的方法
const getC1 = () => {
  //通知分类仓库发请求获取一级分类的数据
  categoryStore.getC1();
};

//一级分类下拉菜单改变事件(选中值时触发)
const handlerChange = () => {
  //改变时清空二、三级分类id
  categoryStore.c2Id = '';
  categoryStore.c3Id = '';
  categoryStore.c3Arr = [];
  //通知仓库获取二级分类数据
  categoryStore.getC2();
};
//二级分类下拉菜单改变事件(选中值时触发)
const handlerChange2 = () => {
  //改变时清空三级分类id
  categoryStore.c3Id = '';
  //通知仓库获取三级分类数据
  categoryStore.getC3();
};
</script>

<style scoped>
.el-form-item {
  width: 25%;
}
</style>
